package com.chushouya.manager.service.common.impl;

import com.chushouya.common.applet.WxClient;
import com.chushouya.common.wx.dto.WxSchemeRequest;
import com.chushouya.common.wx.dto.WxSchemeResponse;
import com.chushouya.manager.dao.entity.ProgramEntity;
import com.chushouya.manager.dao.repository.ProgramRepository;
import com.chushouya.manager.service.common.WxMiniProgramService;
import com.general.framework.core.exception.Ex;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 微信小程序服务实现
 * 
 * @author shouboshi
 * @date 2025-01-23
 */
@Slf4j
@Service
public class WxMiniProgramServiceImpl implements WxMiniProgramService {

    @Resource
    private ProgramRepository programRepository;

    /**
     * 获取小程序信息
     */
    private ProgramEntity selectProgram(String appId) {
        ProgramEntity program = programRepository.selectByAppId(appId);
        if (Objects.isNull(program)) {
            throw Ex.business("小程序信息不存在");
        }
        return program;
    }

    @Override
    public WxSchemeResponse generateScheme(String appId, WxSchemeRequest request) {
        try {
            log.info("生成微信小程序URL Scheme - appId: {}, 请求参数: {}", appId, request);
            
            // 获取小程序配置
            ProgramEntity program = selectProgram(appId);
            
            // 创建 WxClient
            WxClient wxClient = new WxClient(appId, program.getSecret());
            
            // 提取参数
            String path = null;
            String query = null;
            if (request.getJumpWxa() != null) {
                path = request.getJumpWxa().getPath();
                query = request.getJumpWxa().getQuery();
            }
            
            Boolean isExpire = request.getIsExpire();
            Long expireTime = request.getExpireTime();
            
            // 生成 scheme
            String scheme = wxClient.generateScheme(path, query, isExpire, expireTime);
            
            // 构建响应
            WxSchemeResponse response = new WxSchemeResponse();
            response.setErrcode(0);
            response.setErrmsg("ok");
            response.setOpenlink(scheme);
            
            log.info("生成微信小程序URL Scheme成功 - appId: {}, scheme: {}", appId, scheme);
            return response;
            
        } catch (Exception e) {
            log.error("生成微信小程序URL Scheme异常 - appId: {}, error: {}", appId, e.getMessage(), e);
            throw Ex.business("生成URL Scheme异常: " + e.getMessage());
        }
    }

    @Override
    public String generateSchemeSimple(String appId, String path, String query) {
        try {
            log.info("生成永久有效的微信小程序URL Scheme - appId: {}, path: {}, query: {}", appId, path, query);
            
            // 获取小程序配置
            ProgramEntity program = selectProgram(appId);
            
            // 创建 WxClient
            WxClient wxClient = new WxClient(appId, program.getSecret());
            
            // 生成永久有效的 scheme
            String scheme = wxClient.generateScheme(path, query);
            
            log.info("生成永久有效的微信小程序URL Scheme成功 - appId: {}, scheme: {}", appId, scheme);
            return scheme;
            
        } catch (Exception e) {
            log.error("生成永久有效的微信小程序URL Scheme异常 - appId: {}, error: {}", appId, e.getMessage(), e);
            throw Ex.business("生成URL Scheme异常: " + e.getMessage());
        }
    }
}
